1
'****************************** Module Header *******************************
2 ' Module Name: HighLightTextBlock.vb
3 ' Project: VBSL3CustomControl
4 ' Copyright (c) Microsoft Corporation.
6 ' Implementation of custom control HighLightTextBlock.
8 ' This source is subject to the Microsoft Public License.
9 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 ' All other rights reserved.
13 ' * 8/18/2009 8:06 PM Mog Liang Created
14 '****************************************************************************
16 Imports System
.Windows
.Threading
17 Imports System
.ComponentModel
19 <TemplateVisualState(Name
:="NonHighLight", GroupName
:="HightLightStates"), TemplateVisualState(Name
:="HighLight", GroupName
:="HightLightStates")> _
20 Public Class HighLightTextBlock
: Inherits Control
23 ' Register custom control's default style
24 MyBase
.DefaultStyleKey
= GetType(HighLightTextBlock
)
28 Private Sub Dehighlight()
29 VisualStateManager
.GoToState(Me, "NonHighLight", True)
33 ' User call this method to highlight text.
34 Public Sub Highlight()
35 ' Change control state to 'HightLight'
36 VisualStateManager
.GoToState(Me, "HighLight", True)
37 If Me.AutoDehighlight
Then
44 Private Sub InitTimer()
45 Me._timer
= New DispatcherTimer
46 Me._timer
.Interval
= TimeSpan
.FromSeconds(1)
47 ' When time out, change HighLightStates to 'NonHighLight'
48 AddHandler
Me._timer
.Tick
, New EventHandler(AddressOf
Me.Timer_OnTick
)
51 Private Sub Timer_OnTick(ByVal sender
As Object, ByVal e
As EventArgs
)
55 ' Use this property to (de)highlight the text.
56 Private Shared
Sub IsHighlightedChanged(ByVal sender
As DependencyObject
, ByVal e
As DependencyPropertyChangedEventArgs
)
57 Dim newvalue
As Boolean?
= DirectCast(e
.NewValue
, Boolean?
)
58 If newvalue
.Value
Then
59 DirectCast(sender
, HighLightTextBlock
).Highlight()
61 DirectCast(sender
, HighLightTextBlock
).Dehighlight()
63 DirectCast(sender
, HighLightTextBlock
).IsHighlighted
= DirectCast(e
.NewValue
, Boolean?
)
66 Public Overrides
Sub OnApplyTemplate()
67 MyBase
.OnApplyTemplate()
71 ' If this property is set to true, the text will automatically be dehilighted after a specific time. Use the LightTime to control the highlight period.
72 Private _AutoDehighlight
As Boolean
73 Public Property AutoDehighlight() As Boolean
75 Return _AutoDehighlight
77 Set(ByVal value
As Boolean)
78 _AutoDehighlight
= value
82 <TypeConverter(GetType(NullableBoolConverter
))> _
83 Public Property IsHighlighted() As Boolean?
85 Return DirectCast(MyBase
.GetValue(HighLightTextBlock
.IsHighlightedProperty
), Boolean?
)
87 Set(ByVal value
As Boolean?
)
88 MyBase
.SetValue(HighLightTextBlock
.IsHighlightedProperty
, value
)
93 ' Expose LightTime property for highlight time.
94 Public Property LightTime() As TimeSpan
96 If (Me._timer Is
Nothing) Then
99 Return Me._timer
.Interval
101 Set(ByVal value
As TimeSpan
)
102 If (Me._timer Is
Nothing) Then
105 Me._timer
.Interval
= value
109 ' In order to use templatebinding on 'Text' property,
110 ' it must be dependency property.
111 Public Property [Text
]() As String
113 Return CStr(MyBase
.GetValue(HighLightTextBlock
.TextProperty
))
115 Set(ByVal value
As String)
116 MyBase
.SetValue(HighLightTextBlock
.TextProperty
, value
)
122 Private _timer
As DispatcherTimer
123 Public Shared ReadOnly IsHighlightedProperty
As DependencyProperty
= DependencyProperty
.Register("DependencyProperty", GetType(Boolean?
), GetType(HighLightTextBlock
), New PropertyMetadata(False, New PropertyChangedCallback(AddressOf HighLightTextBlock
.IsHighlightedChanged
)))
124 Public Shared ReadOnly TextProperty
As DependencyProperty
= DependencyProperty
.Register("Text", GetType(String), GetType(HighLightTextBlock
), Nothing)